package com.dgut.smart_house.controller;

import com.dgut.smart_house.domain.Handle;
import com.dgut.smart_house.enums.ResultCode;
import com.dgut.smart_house.exception.MyException;
import com.dgut.smart_house.service.HandleService;
import com.dgut.smart_house.util.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
@Slf4j
@RestController
@RequestMapping("/handle/api")
public class HandleApiController {

    @Autowired
    HandleService handleService;

    /**
     * 创建处理
     * @param handle
     * @return
     */
    @RequestMapping("/generateHandle")
    public RestResult generateHandle(Handle handle){
        handleService.generateHandle(handle);
        log.warn("用户-{}-设备-{}-时间-{}-产生异常数据",handle.getFromUser(),handle.getDeviceId(),handle.getExceptionDate(),handle.getDeviceId());
        return new RestResult(ResultCode.SUCCESS,null);
    };

    /**
     * 处理异常
     * @param handleId
     * @return
     */
    @RequestMapping("/checkHandle")
    public RestResult checkHandle(@RequestParam("handleId") int handleId){
        try{
            handleService.checkHandle(handleId);
            Handle handle=handleService.findHandleById(handleId);
            log.info("用户-{}-设备-{}-时间-{}-已处理异常",handle.getFromUser(),handle.getDeviceId(),handle.getCheckTime(),handle.getDeviceId());
        }catch (Exception e){
            throw new MyException(ResultCode.NO_AUTH_FAIL.getCode(),"该处理不存在");
        }
        return new RestResult(ResultCode.SUCCESS,null);
    };

    /**
     * 列出未处理的异常
     * @param session
     * @return
     */
    @RequestMapping("/listUnHandled")
    public RestResult listUnHandled(HttpSession session,
                                    @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                    @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        int fromUser=(int)session.getAttribute("IdFromUserTable");
        return new RestResult(ResultCode.SUCCESS,handleService.listUnHandled(pageNo,pageSize,fromUser));
    };

    /**
     * 列出所有异常信息
     * @param session
     * @return
     */
    @RequestMapping("/listAllHandled")
    public RestResult listAllHandled(HttpSession session,
                                     @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        int fromUser=(int)session.getAttribute("IdFromUserTable");
        return new RestResult(ResultCode.SUCCESS,handleService.listAllHandled(pageNo,pageSize,fromUser));
    };

    /**
     * 列出已经处理的异常
     * @param session
     * @return
     */
    @RequestMapping("/listHandled")
    public RestResult listHandled(HttpSession session,
                                  @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        int fromUser=(int)session.getAttribute("IdFromUserTable");
        return new RestResult(ResultCode.SUCCESS,handleService.listHandled(pageNo,pageSize,fromUser));
    };
}
